package com.util.auth.impl;





public class WebTokenFactoryImpl 
extends TokenFactoryImpl{

	public boolean isValid(String str, long msec) {

		if (str==null || str.isEmpty()) {
			return false;
		}
		
	//	Calendar nowDate= new java.util.GregorianCalendar();
		
	
		long token_time = Long.parseLong(decode_token(str));
		long now = System.currentTimeMillis();
		
		//10分钟内有效
		if (Math.abs(now - token_time ) < msec) {
			
			return true;
		}
		else{
			System.out.println("token is invalid" );
			System.out.println("msec:" + msec);
			System.out.println("now:" + now);
			System.out.println("token_time:" + token_time);
			System.out.println("str:" + str);
			
			return false;
		}
	}

	/**
	 * 最后一位 %3 替换规则标志  
	 * 
	 * 第一位和最后第二位交换，第二位和倒数第三位交换，交换后
	 * 
	 * 如果是规则0
	 * "3" <-> "5"
	 * "2" <-> "4"
	 * "1" <-> "7"
	 * "8" <-> "9"
	 * "0" <-> "6"
	 * 如果是规则1
	 * "0" <-> "5"
	 * "1" <-> "4"
	 * "2" <-> "7"
	 * "3" <-> "9"
	 * "6" <-> "8"
	 * 如果是规则2
	 * "0" <-> "9"
	 * "1" <-> "5"
	 * "2" <-> "8"
	 * "3" <-> "4"
	 * "6" <-> "7"
	 * @param str
	 * @return
	 */
	public static String encode_token(String str){
		
		byte[] temp = str.getBytes();
		int len = temp.length;
		
		if (len <=2) {
			return str;
		}
		
		byte tmp ;
		byte lastOne = temp[temp.length-1];
		for (int i = 0; i < (len-1) / 2; i++) {
			tmp = temp[i];
			temp[i] = temp[temp.length - i - 2] ;
			temp[temp.length - i - 2] = tmp;
		}
		
		
		if (lastOne % 3 == 0) {
			for (int i = 0; i < len-1 ; i++) {
				if (temp[i] == '0') { temp[i] = '6'; continue; }
				if (temp[i] == '1') { temp[i] = '7'; continue; }
				if (temp[i] == '2') { temp[i] = '4'; continue; }
				if (temp[i] == '3') { temp[i] = '5'; continue; }
				if (temp[i] == '4') { temp[i] = '2'; continue; }
				if (temp[i] == '5') { temp[i] = '3'; continue; }
				if (temp[i] == '6') { temp[i] = '0'; continue; }
				if (temp[i] == '7') { temp[i] = '1'; continue; }
				if (temp[i] == '8') { temp[i] = '9'; continue; }
				if (temp[i] == '9') { temp[i] = '8'; continue; }
			}
		}
		else if (lastOne % 3 == 1) {
			for (int i = 0; i < len-1 ; i++) {
				if (temp[i] == '0') { temp[i] = '5'; continue; }
				if (temp[i] == '1') { temp[i] = '4'; continue; }
				if (temp[i] == '2') { temp[i] = '7'; continue; }
				if (temp[i] == '3') { temp[i] = '9'; continue; }
				if (temp[i] == '4') { temp[i] = '1'; continue; }
				if (temp[i] == '5') { temp[i] = '0'; continue; }
				if (temp[i] == '6') { temp[i] = '8'; continue; }
				if (temp[i] == '7') { temp[i] = '2'; continue; }
				if (temp[i] == '8') { temp[i] = '6'; continue; }
				if (temp[i] == '9') { temp[i] = '3'; continue; }
			}
		}
		else if( lastOne % 3 == 2) {
			for (int i = 0; i < len-1 ; i++) {
				if (temp[i] == '0') { temp[i] = '9'; continue; }
				if (temp[i] == '1') { temp[i] = '5'; continue; }
				if (temp[i] == '2') { temp[i] = '8'; continue; }
				if (temp[i] == '3') { temp[i] = '4'; continue; }
				if (temp[i] == '4') { temp[i] = '3'; continue; }
				if (temp[i] == '5') { temp[i] = '1'; continue; }
				if (temp[i] == '6') { temp[i] = '7'; continue; }
				if (temp[i] == '7') { temp[i] = '6'; continue; }
				if (temp[i] == '8') { temp[i] = '2'; continue; }
				if (temp[i] == '9') { temp[i] = '0'; continue; }
			}
		}
		
		return new String(temp);
		
	}
	
	public static String decode_token(String str){
		
		return encode_token(str);
	}




	public String generalToken() //生成token
	{
		//DecodeFactory decodefactory = new DecodeFactoryImpl();//加密解密
	
		//return encrypt(String.valueOf(nowDate.getTimeInMillis()))+"|"+String.valueOf(nowDate.getTimeInMillis());
	
		//Calendar nowDate= new java.util.GregorianCalendar();
		//return encode_token(String.valueOf(nowDate.getTimeInMillis()));
		
		return encode_token(String.valueOf(System.currentTimeMillis()));
	}
}
